<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Picture;

class Category extends Common
{
    public function index()
    {
        /**
         * 添加分类
         */
    	if($_POST){
            $tits = array_filter(explode(",",$_POST['tits']));
    		foreach ($tits as $key => $value) {
    			$data['name'] = $value;
    			$data['parentsid'] = $_POST['pid'];
	    		$data['sort'] = $_POST['sort'];
	    		$res = db('category')->insert($data);
    		}
    		if($res){
	    		$this->success('分类添加成功');
	    	}else{
                $this->error('分类添加失败');
            }
    	}

        /**
         * 分类显示输出
         * 
         */
    	$catedata = db('category')->order('sort')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);
    	$this->assign('cate',$cate);
        return view();
    }


    public function catedel($id){
        //删除当前以及以下分类
        $data = db('category')->select();
        $delcate = new AdminModel();
        $delid = $delcate->piccateid($data,$id);
        $delid[] = intval($id);

        //删除分类下的所有信息
        $deldata = db('picture')->where('cateid','in',$delid)->select();
        if ($deldata) {
            foreach ($deldata as $k => $v) {
                $delpicid[] = $v['id'];
            }
            $delpic = new Picture();
            $respic = $delpic->delpic($delpicid);
        }
        
        $rescate = db('category')->delete($delid);
        $this->success('删除成功');
    }

    public function cateedit($id){
        if($_POST){
           $res = db('category')->where('cateid',$id)->update($_POST);
           if($res){
                $this->success('修改成功',url('Category/index'));
           }else{
                $this->error('修改失败',url('Category/index'));
           }
        }

        $iddata = db('category')->where('cateid',$id)->find();
        $this->assign('iddata',$iddata);
        $catedata = db('category')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);
        $this->assign('cate',$cate);
        return view();
    }

    public function add()
    {
        // 获取小图
        $smallpic = request()->file('smallpic');
        if($smallpic){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $small = $smallpic->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/smallpic','');    
            if($small){
                $_POST['smallpic'] =  '/public/uploads/smallpic/'.$small->getFilename();
            }else{
                // 上传失败获取错误信息
                return $this->error($small->getError());
            }
        }

        // 获取多图
        $bigpic = request()->file('bigpic');
        if ($bigpic) {
            foreach($bigpic as $bigpic){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $big = $bigpic->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/bigpic','');
                if($big){
                    // 成功上传后 获取上传信息
                    $bigpic_arr[] = '/public/uploads/bigpic/'.$big->getFilename();
                }else{
                    // 上传失败获取错误信息
                    return $this->error($big->getError());
                }    
            }
            $_POST['alt'] = implode('|',$_POST['alt']);
            $_POST['bigpic'] = implode('|',$bigpic_arr);
        }
        
        if (request()->isPost()) {
            if(isset($_POST['position'])){
                $_POST['position'] = implode(',',$_POST['position']);
            }
            $_POST['time'] = date('Y-m-d',time());
            $res = db('picture')->insert($_POST);
            if ($res) {
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }

        $catedata = db('category')->order('sort')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);
        $this->assign('cate',$cate);
        return view();
    }

    public function manage($page=1)
    {
        $each = 10;  //每页显示多少条
        @$dbtable = picture;   //数据表名
        $pages = new AdminModel();
        $res = $pages->picpage($page,$each,$dbtable);
        if($res==-1){
            $this->error('没有此页面');
        }

        $catedata = db('category')->order('sort')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);

        $this->assign(array(
            'list' => $res['list'],
            'pagetotal' => $res['pagetotal'],
            'page' => $res['page'],
            'cate' => $cate
        ));
        return view();
    }

    public function del($id)
    {
        $delpic = new Picture();
        $res = $delpic->delpic($id);
        if ($res) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function sorts($id,$picsort)
    {
        foreach ($id as $k => $v) {
            $res[] = db('picture')->where('id',$id[$k])->update(['picsort' => $picsort[$k]]);
        }
        if (in_array(1, $res)) {
            $this->success('排序成功');
        }else{
            $this->error('排序失败' );
        }
    }

    public function home($id)
    {   
        $res = false;
        foreach ($id as $k => $v) {
            $list = db('picture')->where('id',$v)->select();
            $position = explode(',',$list[0]['position']);
            if(!in_array('1',$position)){
               $position[] = '1'; 
               $position = implode(',',array_unique(array_filter($position)));
               $res = db('picture')->where('id',$list[0]['id'])->update(['position' => $position]);
            }
        }
        if ($res) {
            $this->success('设置首页成功');
        }else{
            $this->error('设置首页失败' );
        }
    }

    public function edit($id)
    {   
        // 获取小图
        $smallpic = request()->file('smallpic');
        if($smallpic){
            // 移动到框架应用根目录/public/uploads/ 目录下
            $small = $smallpic->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/smallpic','');   
            $s_pic = db('picture')->where('id',$id)->value('smallpic');   //获取原来图片地址 
            if($small){
                $_POST['smallpic'] =  '/public/uploads/smallpic/'.$small->getFilename();
                //删除原来的logo图片
                $thumbpath=$_SERVER['DOCUMENT_ROOT'].$pic;
                if(file_exists($thumbpath)){
                    @unlink($thumbpath);
                }
            }else{
                // 上传失败获取错误信息
                return $this->error($small->getError());
            }
        }

        // 获取多图
        $bigpic = request()->file('bigpic');
        if ($bigpic) {
            foreach($bigpic as $bigpic){
                // 移动到框架应用根目录/public/uploads/ 目录下
                $big = $bigpic->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/bigpic','');

                if($big){
                    // 成功上传后 获取上传信息
                    $_POST['bigpic'][] = '/public/uploads/bigpic/'.$big->getFilename();
                }else{
                    // 上传失败获取错误信息
                    return $this->error($big->getError());
                }    
            }
        }

        if (request()->isPost()) {
            $oldpic = db('picture')->where('id',$id)->value('bigpic');   //获取原来图片地址 
            $oldpic = explode('|',$oldpic);
            

            if($_POST['alt']){
                if (is_array($_POST['alt'])) {
                    $_POST['alt'] = implode('|',$_POST['alt']);
                }
            }

            if($_POST['bigpic']){
                $newpic = array_diff($oldpic,$_POST['bigpic']);
                if ($newpic) {
                    //删原图
                    foreach ($newpic as $k => $v) {
                        $thumbpath=$_SERVER['DOCUMENT_ROOT'].$v;
                        if(file_exists($thumbpath)){
                            @unlink($thumbpath);
                        }
                    }
                }
                if (is_array($_POST['bigpic'])) {
                    $_POST['bigpic'] = implode('|',$_POST['bigpic']);
                }
            }

            if(isset($_POST['position'])){
                $_POST['position'] = implode(',',$_POST['position']);
            }

            $res = db('picture')->where('id',$id)->update($_POST);
            if ($res) {
                $this->success('修改成功','Category/manage');
            }else{
                $this->error('修改失败');
            }
        }

        //修改信息输出
        $list = db('picture')->where('id',$id)->find();

        //大图+描述输出
        $bigpic = explode('|',$list['bigpic']);
        $alt = explode('|',$list['alt']);
        foreach ($bigpic as $k => $v) {
            $big[$k]['bigpic'] = $bigpic[$k];
            $big[$k]['alt'] = $alt[$k];
        }

        //分类输出
        $catedata = db('category')->order('sort')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);

        $this->assign(array(
            'cate' => $cate,
            'list' => $list,
            'big' => $big
        ));
        return view();
    }

    public function vouch($id)
    {   
        $res = false;
        foreach ($id as $k => $v) {
            $list = db('picture')->where('id',$v)->select();
            $position = explode(',',$list[0]['position']);
            if(!in_array('2',$position)){
               $position[] = '2'; 
               $position = implode(',',array_unique(array_filter($position)));
               $res = db('picture')->where('id',$list[0]['id'])->update(['position' => $position]);
            }
        }
        if ($res) {
            $this->success('推荐成功');
        }else{
            $this->error('推荐失败' );
        }
    }

    public function top($id)
    {   
        $res = false;
        foreach ($id as $k => $v) {
            $list = db('picture')->where('id',$v)->select();
            $position = explode(',',$list[0]['position']);
            if(!in_array('3',$position)){
               $position[] = '3'; 
               $position = implode(',',array_unique(array_filter($position)));
               $res = db('picture')->where('id',$list[0]['id'])->update(['position' => $position]);
            }
        }
        if ($res) {
            $this->success('置顶成功');
        }else{
            $this->error('置顶失败' );
        }
    }

    public function changecate($id,$movecid)
    {
        $changecate = new Picture();
        $res = $changecate->changecate($id,$movecid);
        if ($res) {
            $this->success('移动成功');
        }else{
            $this->error('移动失败');
        }
    }

    public function search($page=1)
    {
        $each = 10;  //每页显示多少条
        @$dbtable = picture;   //数据表名
        $pages = new Picture();
        $res = $pages->search($page,$each,$dbtable,$_POST);
        if($res==-1){
            $this->error('没有此页面');
        }

        $catedata = db('category')->order('sort')->select();
        $category = new AdminModel();
        $cate = $category->category($catedata);

        $this->assign(array(
            'list'      => $res['list'],
            'pagetotal' => $res['pagetotal'],
            'page'      => $res['page'],
            'cate'      => $cate
        ));
        return view();
    }

}
